package com.masterclient.app.businessmodule.vipmodule.financial_management.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.activity.FCBaseActivity;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.financial_management.adapter.RechargeRecordAdapter;
import com.masterclient.app.businessmodule.vipmodule.financial_management.bean.RechargeRecordData;
import com.masterclient.app.businessmodule.vipmodule.financial_management.contract.RechargeRecordContract;
import com.masterclient.app.businessmodule.vipmodule.financial_management.contract.RechargeRecordPresenter;

import java.math.BigDecimal;

import butterknife.BindView;

@Route(path = FCRouterPath.CASH_RECORD)
public class CashRecordActivity extends FCBaseActivity<RechargeRecordPresenter> implements RechargeRecordContract.View {

    @BindView(R.id.cash_record_in_total)
    TextView cashRecordInTotal;
    @BindView(R.id.cash_record_already_total)
    TextView cashRecordAlreadyTotal;
    @BindView(R.id.cash_record_refresh)
    SwipeRefreshLayout cashRecordRefresh;
    @BindView(R.id.cash_record_recycler)
    RecyclerView cashRecordRecycler;

    @Autowired(name = "source")
    String source="";

    private RechargeRecordAdapter recordAdapter ;
    private int page = 1;
    private View emptyView;
    TextView tv_base_state;
    TextView tv_base_state_refresh;
    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_cash_record;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("提现记录");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        cashRecordRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        recordAdapter = new RechargeRecordAdapter();

        emptyView = View.inflate(getActivity(), R.layout.view_base_state, null);
        ImageView imageView = emptyView.findViewById(R.id.img_base_state);
        imageView.setImageResource(R.drawable.iconempty_list);
        tv_base_state = emptyView.findViewById(R.id.tv_base_state);
        tv_base_state_refresh = emptyView.findViewById(R.id.tv_base_state);
        emptyView.setOnClickListener(v -> {
            emptyView.setVisibility(View.GONE);
            mPresenter.getData(page,source);
        });
        emptyView.setVisibility(View.GONE);
        recordAdapter.setEmptyView(emptyView);
        recordAdapter.bindToRecyclerView(cashRecordRecycler);

        recordAdapter.setOnLoadMoreListener(()->{
            mPresenter.getData(page,source);
        },cashRecordRecycler);

        cashRecordRefresh.setOnRefreshListener(()->{
            page = 1;
            mPresenter.getData(page,source);
        });


        mPresenter.getData(page,source);

    }

    @Override
    public void showData(RechargeRecordData data) {
        cashRecordRefresh.setRefreshing(false);
        recordAdapter.loadMoreComplete();

        cashRecordInTotal.setText(Utils.decimalFormat(new BigDecimal(data.getWait_total())));
        cashRecordAlreadyTotal.setText(Utils.decimalFormat(new BigDecimal(data.getSuccess_total())));

        if (page == 1){
            if (data.getList() != null && data.getList().size()>0){
                recordAdapter.setNewData(data.getList());
            }else {
                recordAdapter.getData().clear();
            }
        }else {
            recordAdapter.addData(data.getList());
        }
        if(data.isHas_more()){
            ++page;
        }else {
            recordAdapter.setEnableLoadMore(false);
        }
        showEmpty("暂无记录");
    }

    @Override
    public void failureData(String msg) {
        cashRecordRefresh.setRefreshing(false);
        recordAdapter.loadMoreComplete();
        showEmpty(msg);
    }

    private void showEmpty(String msg){
        if(recordAdapter != null && recordAdapter.getData().size()>0){
            emptyView.setVisibility(View.GONE);
        }else {
            tv_base_state.setText(msg);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

}
